package org.vaadinpa.data;

import org.vaadinpa.logic.Ticker;

public class PlanetaryScan implements Comparable<PlanetaryScan> {

	public Planet planet;
	public int tick;
	public TechLevel techLevelAtScan;

	public PlanetaryScan(Planet p, TechLevel l) {
		planet = p.clone();
		techLevelAtScan = l;
		tick = Ticker.currentTick;
	}

	public String getHeader() {
		return "Planetary Scan of " + planet.name + " " + planet.coords + ", level " + (techLevelAtScan.ordinal() + 1)
				+ ", tick " + tick;
	}

	public String getNewsReportHTML() {
		StringBuilder sb = new StringBuilder();
		sb.append("Planet:" + planet.name + "<br/>");
		sb.append("Ruler:" + planet.ruler + "<br/>");

		if (techLevelAtScan == TechLevel.LOWEST) {
			sb.append("Total amount of ships:" + planet.getTotalShips() + "<br/>");
			sb.append("Total amount of roids:" + planet.getTotalRoids() + "<br/>");
		} else if (techLevelAtScan == TechLevel.LOW) {
			sb.append("Total amount of ships:" + planet.getTotalShips() + "<br/>");
			for (Resource r : Resource.valuesWithoutNew()) {
				sb.append("Total amount of " + r.toString() + " roids:" + planet.roids.get(r) + "<br/>");
			}
		} else if (techLevelAtScan == TechLevel.MODERATE) {
			for (Ship s : Ship.getAvailableShips()) {
				if (planet.getTotalShips(s) != 0) {
					sb.append("Total amount of ships of type " + s.getName() + ":" + planet.getTotalShips(s) + "<br/>");
				}
			}
			for (Resource r : Resource.valuesWithoutNew()) {
				sb.append("Total amount of " + r.toString() + " roids:" + planet.roids.get(r) + "<br/>");
			}
			sb.append("Planet tech level is " + planet.level);
		} else if (techLevelAtScan == TechLevel.HIGH) {
			for (Fleet f : planet.fleets) {
				sb.append("Fleet named '" + f.name + "'.<br/>");
				if (f.eta != 0) {
					sb.append("In transit ");
				} else {
					sb.append("Location ");
				}
				sb.append("with orders to " + f.currentOrder + "<br/>");
				sb.append("Fleet named '" + f.name + "'.<br/>");
				sb.append("Total of " + f.numShips() + " ships:<br/>");
				for (Ship s : Ship.getAvailableShips()) {
					if (f.get(s) != 0) {
						sb.append("Total amount of ships of type " + s.getName() + ":" + f.get(s) + "<br/>");
					}
				}
			}
			for (Resource r : Resource.valuesWithoutNew()) {
				sb.append("Total amount of " + r.toString() + " roids:" + planet.roids.get(r) + "<br/>");
			}
			sb.append("Planet tech level is " + planet.level);
		} else if (techLevelAtScan == TechLevel.HIGHEST) {
			for (Fleet f : planet.fleets) {
				sb.append("Fleet named '" + f.name + "'.<br/>");
				if (f.eta != 0) {
					sb.append("In transit(eta " + f.eta + " ticks) to ");
				} else {
					sb.append("Location ");
				}
				sb.append(f.location + " with orders to " + f.currentOrder + "<br/>");

				sb.append("Total of " + f.numShips() + " ships:<br/>");
				for (Ship s : Ship.getAvailableShips()) {
					if (f.get(s) != 0) {
						sb.append("Total amount of ships of type " + s.getName() + ":" + f.get(s) + "<br/>");
					}
				}
			}
			for (Resource r : Resource.valuesWithoutNew()) {
				sb.append("Total amount of " + r.toString() + " roids:" + planet.roids.get(r) + "<br/>");
			}
			sb.append("Planet tech level is " + planet.level);
		}
		return sb.toString();
	}

	public int compareTo(PlanetaryScan o) {
		if (tick == o.tick) {
			return planet.name.compareTo(o.planet.name);
		}
		// newer first
		return o.tick - tick;
	}

	public void serialize(StringBuilder sb) {
		sb.append("<scan techLevel=\"\" tick=\"\">");
		sb.append("<planet x=\"" + planet.coords.x + "\" y=\"" + planet.coords.y + "\"/>");
		sb.append("</scan>");
	}

}
